<template>
	<div class="system-managerUser-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="managerUserDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账号" prop="username">
							<el-input v-model="state.ruleForm.username" placeholder="请输入账号" clearable :disabled="state.ruleForm.isDelete==1"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="姓名" prop="nickName">
							<el-input v-model="state.ruleForm.nickName" placeholder="请输入用户姓名" clearable :disabled="state.ruleForm.isDelete==1"></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="电话号码" prop="userMobile">
              <el-input v-model="state.ruleForm.userMobile" placeholder="请输入用户电话" clearable :disabled="state.ruleForm.isDelete==1"></el-input>
            </el-form-item>
          </el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.dialog.type=='add'">
						<el-form-item label="关联角色" prop="userType">
							<el-select v-model="state.ruleForm.userType" placeholder="请选择" clearable class="w100"  :disabled="state.ruleForm.isDelete==1">
								<el-option label="运营管理人员" :value="0"></el-option>
<!--                <el-option label="工作人员" :value="4"></el-option>-->
<!--                <el-option label="开发或运维人员" :value="100"></el-option>-->
							</el-select>
						</el-form-item>
					</el-col>

<!--					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">-->
<!--						<el-form-item label="账户密码">-->
<!--							<el-input v-model="state.ruleForm.password" placeholder="请输入" type="password" clearable></el-input>-->
<!--						</el-form-item>-->
<!--					</el-col>-->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
              <el-select v-model="state.ruleForm.isDelete" placeholder="请选择"  class="w100" :disabled="state.ruleForm.isDelete==0">
                <el-option label="正常"  :value="0"></el-option>
                <el-option label="禁用"  :value="1"></el-option>
              </el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(managerUserDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref} from 'vue';
import {userApi} from "/@/api/user";
import {ElMessage, FormInstance, FormRules} from 'element-plus'

const {saveOrUpdateManager} = userApi();
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const rules = reactive<FormRules<RowManagerUserType>>({
  username: [
    { required: true, message: '请输入学号', trigger: 'blur' },
    //{ min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  nickName: [
    {
      required: true,
      message: '请输入姓名',
      trigger: 'blur',
    },
  ],
  userType: [
    {
      required: true,
      message: '请选择角色',
      trigger: 'change',
    },
  ],

  orgId: [
    {
      required: true,
      message: '请选择科室',
      trigger: 'change',
    },
  ],


})

// 定义变量内容
const managerUserDialogFormRef = ref();
const state = reactive({
	ruleForm: {
    id: '',
    username: '',
    orgId: '1',
    orgName: '',
    nickName: '',
    userMobile:'',
    userRole:'',
    userType:0,
    gmtCreate: '',
    isDelete: 0,
    gmtModified: '',
	},
  sysOrgData: [] as OrgTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowManagerUserType) => {
  clearForm();
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改管理员信息';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增管理员';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
  state.dialog.type = type;
	getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
// const onSubmit = () => {
// 	closeDialog();
// 	emit('refresh');
// 	// if (state.dialog.type === 'add') { }
// };
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const res = await saveOrUpdateManager(state.ruleForm)
      if (res.status === 200) {
        ElMessage.success('更改成功');
        emit('refresh');
      }else{
        ElMessage.error('更改失败');
      }
      closeDialog();

    }
  })
}

// 初始化机构数据
const getMenuData = async () => {

};

const clearForm = () => {
  state.ruleForm = {
    id: '',
    username: '',
    orgId: '1',
    orgName: '',
    nickName: '',
    userMobile:'',
    userRole:'',
    userType:0,
    gmtCreate: '',
    isDelete: 0,
    gmtModified: '',
  }
}
// 暴露变量
defineExpose({
	openDialog,
});
</script>
